package com.rslai.base.tool.servertest.util;

import com.rslai.base.tool.servertest.command.StepCommand;
import com.rslai.base.tool.servertest.config.StepConfig;
import com.rslai.base.tool.servertest.model.KeyValueStore;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class CloneUtil {
    public static List<KeyValueStore> cloneKeyValueStore(List<KeyValueStore> parameters) {
        if (parameters == null) {
            return null;
        }
        return (List) clone(parameters);
    }

    private static Object clone(Object value) {
        if ((value instanceof Map)) {
            Map mapValue = (Map) value;

            Map newMap = new LinkedHashMap();
            for (Iterator i$ = mapValue.keySet().iterator(); i$.hasNext(); ) {
                Object o = i$.next();
                newMap.put(o, clone(mapValue.get(o)));
            }

            return newMap;
        }
        if ((value instanceof List)) {
            List parameters = (List) value;
            List result = new ArrayList();
            for (Iterator i$ = parameters.iterator(); i$.hasNext(); ) {
                Object item = i$.next();
                result.add(clone(item));
            }
            return result;
        }
        if ((value instanceof KeyValueStore)) {
            KeyValueStore old = (KeyValueStore) value;
            return new KeyValueStore(old.getName(), clone(old.getValue()));
        }
        return value;
    }

    public static List<StepCommand> cloneStepCommand(List<StepCommand> children) {
        List result = new ArrayList();
        if (children != null) {
            for (StepCommand sc : children) {
                result.add(sc.cloneCommand());
            }
        }
        return result;
    }

    public static List<StepConfig> cloneStepConfig(List<StepConfig> children) {
        List result = new ArrayList();
        if (children != null) {
            for (StepConfig sc : children) {
                result.add((StepConfig) sc.clone());
            }
        }
        return result;
    }
}
